Capture Screenshot for Failed Tests in TestNG
TestNG provides a feature to capture screenshot for failed tests. This feature is very useful when you want to see the actual error in the browser. Here is how you can do it.
Steps to Run the Method
- Create a Package Name "screensclick" and in that package Create one Class and name it "ScreenShotTest"
- And then After Write the given below code
package screensclick; import java.io.File; import java.io.IOException; import java.util.Date; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; //import org.openqa.selenium.chrome.ChromeDriver; import common.BaseTest; //import io.github.bonigarcia.wdm.WebDriverManager; public class ScreenShotTest extends BaseTest { public static void getScreenshot() throws IOException { Date currentDate = new Date(); String screenShotFileName = currentDate.toString().replace(" ", "-").replace(":", "-"); File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File(".//screenshot/"+screenShotFileName+".png")); } }
- After that you get the error because in the above code we doesn't provided the browser driver so
- then next step is to give browser driver to the previous class with new class
- create new class in common package name it "BaseTest" or as your wish etc... And then write the given below code
package common; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterSuite; import org.testng.annotations.BeforeSuite; import io.github.bonigarcia.wdm.WebDriverManager; public class BaseTest { public static WebDriver driver = null; @BeforeSuite public void launchbrowser() { WebDriverManager.firefoxdriver().setup(); driver = new FirefoxDriver(); } @AfterSuite public void closebrowser() { driver.close(); } }
- This code is already extended in the previous code so no need to extends manually
- and Then create new class in a general package where you are creating all this previous methods, name it " what ever you want to be " , and Write the given below code
package asc; import org.testng.Assert; import org.testng.annotations.Test; import common.BaseTest; public class ScreenShotDemoTest extends BaseTest { @Test public void launchApp() { driver.get("https://ittrainingclasses.in/"); Assert.assertTrue(false); } }
- And then Change the Listener class code as per given below
package common; import java.io.IOException; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestResult; import screensclick.ScreenShotTest; //import asc.ScreenShotTest; public class Listeners extends ScreenShotTest implements ITestListener{ public void onTestStart(ITestResult result) { System.out.println("Test case is Starting"); } public void onTestSuccess(ITestResult result) { } public void onTestFailure(ITestResult result) { System.out.println("Test Failed - Screenshot Captured"); try { getScreenshot(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void onTestSkipped(ITestResult result) { } public void onTestFailedButWithinSuccessPercentage(ITestResult result) { } public void onStart(ITestContext context) { } public void onFinish(ITestContext context) { } }
- And After that create one Test Suite and change the code as per given below code
< ?xml version="1.0" encoding="UTF-8"?> < !DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> < suite name="Listeners Test Suite"> < listeners> < listener class-name="common.Listeners" /> < /listeners> < test name="Listener Testing"> < parameter name="browser" value="edge">< /parameter> < classes> < class name="asc.ScreenShotDemoTest"/> < /classes> < /test> < /suite>
- After all this process done then Run the Test Suite